package GameLogic

import (
	"PBMessage"
	"time"

	"git.oschina.net/yangdao/extlib/math3d"

	"git.oschina.net/yangdao/extlib/log"
	"github.com/golang/protobuf/proto"
)

type idleState struct {
	*genericState
}

func (idle *idleState) onEnter(args ...interface{}) {
	state := &PBMessage.SC_UnitState{}
	state.UnitId = proto.Int64(idle.gameUnit.Unit.GUID)
	state.State = proto.Int32(0)
	state.Time = proto.Int64(time.Now().UnixNano() / 1e6)
	idle.gameUnit.Scene.(*GameScene).sendEachPlayer(PBMessage.PBID_SC_UnitState, state)
	log.Log("GameLogic,Info", "单位进入站立状态")
}

func (idle *idleState) onUpdate(dt int) {
	cmd, tuple := idle.gameUnit.getCommond()
	switch cmd {
	case CMD_MOVE:
		state := tuple[3].(int32)
		if state == 1 {
			idle.gameUnit.isMoveing = true
			idle.gameUnit.Unit.Dir = math3d.Vec3{X: float64(tuple[1].(int32)), Z: float64(tuple[2].(int32))}
			idle.gameUnit.Unit.Dir.Normalize()
			idle.gameUnit.preMovePos = idle.gameUnit.Unit.Pos()
			idle.gameUnit.startMoveTime = tuple[4].(int64)
			idle.gameUnit.chageState("run")
		}
	case CMD_RUNABILITY:
		idle.gameUnit.Unit.CurAbility = idle.gameUnit.Unit.Abilitys[tuple[1].(string)]
		if idle.gameUnit.Unit.CurAbility.Base().IsCanCast() {
			idle.gameUnit.chageState("canRunAbility")
		}
	}
}
